package cn.iocoder.yudao.module.erp.controller.admin.warehouserule;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.warehouserule.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.warehouserule.WarehouseRuleDO;
import cn.iocoder.yudao.module.erp.service.warehouserule.WarehouseRuleService;

@Tag(name = "管理后台 - ERP 库房规则")
@RestController
@RequestMapping("/erp/warehouse-rule")
@Validated
public class WarehouseRuleController {

    @Resource
    private WarehouseRuleService warehouseRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 库房规则")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-rule:create')")
    public CommonResult<Long> createWarehouseRule(@Valid @RequestBody WarehouseRuleSaveReqVO createReqVO) {
        return success(warehouseRuleService.createWarehouseRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 库房规则")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-rule:update')")
    public CommonResult<Boolean> updateWarehouseRule(@Valid @RequestBody WarehouseRuleSaveReqVO updateReqVO) {
        warehouseRuleService.updateWarehouseRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 库房规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warehouse-rule:delete')")
    public CommonResult<Boolean> deleteWarehouseRule(@RequestParam("id") Long id) {
        warehouseRuleService.deleteWarehouseRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 库房规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-rule:query')")
    public CommonResult<WarehouseRuleRespVO> getWarehouseRule(@RequestParam("id") Long id) {
        WarehouseRuleDO warehouseRule = warehouseRuleService.getWarehouseRule(id);
        return success(BeanUtils.toBean(warehouseRule, WarehouseRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 库房规则分页")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-rule:query')")
    public CommonResult<PageResult<WarehouseRuleRespVO>> getWarehouseRulePage(@Valid WarehouseRulePageReqVO pageReqVO) {
        PageResult<WarehouseRuleDO> pageResult = warehouseRuleService.getWarehouseRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 库房规则 Excel")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseRuleExcel(@Valid WarehouseRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseRuleDO> list = warehouseRuleService.getWarehouseRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 库房规则.xls", "数据", WarehouseRuleRespVO.class,
                        BeanUtils.toBean(list, WarehouseRuleRespVO.class));
    }

}